<?php
/**
 * Created by PhpStorm.
 * User: leo
 * Date: 2018/5/15
 * Time: 16:32
 */
include "../classes/logincheck.php";
include '../classes/conn.php';
if(islogin()!='teacher_login'){
    die('error');
}
$title=$_POST['title'];
$class=$_POST['class'];
$time=$_POST['time'];
$notice='';
$deadline="";
$sec="";
$sub="";
$attach="";
if(isset($_POST['notice'])){
    $notice=$_POST['notice'];
}
if(isset($_POST['deadline'])){
    $deadline=$_POST['deadline'];
}
if(isset($_POST['sec'])){
    $sec=$_POST['sec'];
}
if(isset($_POST['sub'])){
    $sub=$_POST['sub'];
}
if(isset($_POST['attach'])){
    $attach=$_POST['attach'];
}

$query="insert into homework(title,notice,time,deadline,class,section,subsection,attach)
values ('{$title}','{$notice}','{$time}','{$deadline}','{$class}','{$sec}','{$sub}','{$attach}')";
mysql_query($query);

$id=mysql_insert_id();

if(is_uploaded_file($_FILES['attach']['tmp_name'])){

    $file=$_FILES['attach']['name'];
    $dotindex=strrpos($file,'.');//最后一次‘.’出现的位置
    $sufname=substr($file,$dotindex+1);//
    $file=iconv('UTF-8','gb2312',$id.'.'.$sufname);

    move_uploaded_file($_FILES['attach']['tmp_name'],
        "../Files/" . $_SESSION['t_num'] . "/homework/attach/" .$file);
    $attach="requests/Files/" . $_SESSION['t_num'] . "/homework/attach/" .$file;

    $query="update homework set attach='{$attach}' where Id='{$id}'";
    mysql_query($query);

}
if(is_uploaded_file($_FILES['answer']['tmp_name'])){

    $file=$_FILES['attach']['name'];
    $dotindex=strrpos($file,'.');//最后一次‘.’出现的位置
    $sufname=substr($file,$dotindex+1);//
    $file=$id.'.'.$sufname;

    move_uploaded_file($_FILES['answer']['tmp_name'],
        "../Files/" . $_SESSION['t_num'] . "/homework/answer/answer" .$file);
    $answer="requests/Files/" . $_SESSION['t_num'] . "/homework/answer/answer" .$file;

    $query="update homework set answer='{$answer}' where Id='{$id}'";
    mysql_query($query);
}
echo '1';

